﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreesenseSubAIS.Core
{
    internal class DeviceControl0085 : BaseDeviceControl
    {
        private readonly DeviceParameter _deviceParameter;
        private ETSHelper _ets;
        public DeviceControl0085(DeviceParameter dp)
        {
            _deviceParameter = dp;
            Init();
        }

        private void Init()
        {
            try
            {
                InitEle();
                InitCamera();
                SideUpLightS = DeviceStatusEnum.Normal;
                SideDownLightS = DeviceStatusEnum.Normal;
                BackLightS = DeviceStatusEnum.Normal;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(string.Format( "初始化失败硬件！\n{0}",ex.Message));
            }
        }

        internal void InitEle()
        {
            _ets = new ETSHelper();
            _ets.OpenService(_deviceParameter.DefaultProductId);
            ElectricS = DeviceStatusEnum.Normal;
        }

        private void InitCamera()
        {
            try
            {
                if (Properties.Settings.Default.CameraType == 1)
                {
                    Camera = new CameraEbus(_deviceParameter.Camera0Info);
                    Camera.Init();
                }
                else
                {
                    Camera = new CameraEuresys(Properties.Settings.Default.CameraIndex );
                    Camera.Init();
                }
                if (Camera != null && Camera.CameraState)
                {
                    if (ImageReceiveed != null) Camera.ImageReady += ImageReceiveed;
                    CameraS = DeviceStatusEnum.Normal;
                }
                else
                {
                    CameraS = DeviceStatusEnum.Unknow;
                }
            }
            catch (Exception)
            {
                CameraS = DeviceStatusEnum.Unknow;
            }
        }

        #region 光源部分在主机
        public override void OpenUpSideLight(byte inten)
        {

        }

        public override void OpenDownSideLight(byte inten)
        {

        }

        public override void CloseUpSideLight()
        {

        }

        public override void CloseDownSideLight()
        {

        }

        public override void OpenBackLight(byte inten)
        {

        }
        public override void CloseBackLight()
        {

        }
        #endregion 光源部分在主机
        public override void ChangeColor(int index)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                if (index >= -1)
                    _ets.ChangeColor(index == -1 ? GetCurrentindex() : index);
                sw.Stop();
            }
            catch (Exception ex)
            {
                ElectricS = DeviceStatusEnum.UnNormal;
                BpjbCommon.Net.Core.BpEventHandler.ErrHandler("DeviceControl0085.ChangeColor(index)-Error：" + ex.Message, BpjbCommon.Net.Core.ErrType.Warning);
            }
        }

        private int GetCurrentindex()
        {
            int index = 0;
            if (_ets != null)
                index = GetPixIndex();
            return index;
        }

        private int GetPixIndex()
        {
            return _ets.PixIndex;
        }

        public override bool Begin()
        {
          return   EtsPowerOn();
        }

        public override void End()
        {
            ShutDown();
        }
        public bool EtsPowerOn()
        {
            try
            {
                return _ets.PowerOn();
            }
            catch (Exception ex)
            {
                ElectricS = DeviceStatusEnum.UnNormal;
                BpjbCommon.Net.Core.BpEventHandler.ErrHandler("DeviceControl0085.ETSPowerOn()-Error：" + ex.Message, BpjbCommon.Net.Core.ErrType.Warning);
            }
            return false;
        }
        public void ShutDown()
        {
            try
            {
                _ets.ShotDown();
            }
            catch (Exception ex)
            {
                ElectricS = DeviceStatusEnum.UnNormal;
                BpjbCommon.Net.Core.BpEventHandler.ErrHandler("DeviceControl0085.ShutDown()-Error：" + ex.Message, BpjbCommon.Net.Core.ErrType.Warning);
            }
        }
    }
}
